/*
 * Copyright (c) 2017. All  rights reserved.
 * 项目名：microservice-base
 * 文件名：SpecialDateEditor.java
 * Date  ：17-12-4 上午10:59
 * Author：abin
 *
 */

package com.microservice.base.common;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class SpecialDateEditor extends PropertyEditorSupport {
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if (StringUtils.isBlank(text)) {
                // Treat empty String as null value.
                setValue(null);
            } else {
                date = format.parse(text);
            }
        } catch (ParseException e) {
            format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = format.parse(text);
            } catch (ParseException e1) {
                format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");

                try {
                    date = format.parse(text);
                } catch (Exception e2) {
                    format = new SimpleDateFormat("yy/MM/dd");
                    try {
                        date = format.parse(text);
                    } catch (Exception e3) {
                        format = new SimpleDateFormat("yyyy年MM月dd日");
                        try {
                            date = format.parse(text);
                        } catch (Exception e4) {
                            logger.error("自动绑定日期数据出错", e);
                        }
                    }
                }
            }
        }
        setValue(date);

    }

}
